from flask import request, jsonify
from apps.project.business.testcase import TestCaseBusiness
from library.api.tBlueprint import tblueprint
import json

bpname = 'x_mind'
x_mind = tblueprint(bpname, __name__)


@x_mind.route('/check_title', methods=['POST'])
def check_case_content():
    """校验用例导入内容
    /v1/project/x_mind/check_title
    :return:{
                "msg":{
                    "rs":[
                        {
                            "case_title":"员工列表页面数据查看",
                            "module":"7186",
                            "precondition":"进入erp员工列表页面",
                            "pri":"1"
                        },
                        {
                            "case_title":"员工列表搜索查询",
                            "module":"7186",
                            "precondition":null,
                            "pri":"2"
                        }],
                    "title":"机构后台回归测试用例"
                },
                "status":0
            }

    """
    # 获取数据
    data = request.files
    file = data['file']
    msg, content, code = TestCaseBusiness.check_case_content(file, request.form.get('type'))
    
    return jsonify({"msg": json.dumps(msg, ensure_ascii=False), 'rs': content, "code": code})


@x_mind.route('/upload', methods=['POST'])
def x_mind_import():
    """导入用例
    /v1/project/x_mind/upload
    :return:
    """
    # 获取import_type类型,组装
    import_type = request.args.get('path').split('import_type')[1]
    path = request.args.get('path').split('#import_type')[0]
    if path is None:
        return jsonify({"msg": '上传文件路径不存在', "code": 400})
    msg, content, code = TestCaseBusiness.import_case(path, import_type)
    return jsonify({"msg": msg, 'rs': content, "code": code})


@x_mind.route('/sync/zt/case', methods=['POST'])
def sync_zt_case():
    """确定导入用例
    /v1/project/x_mind/sync/zt/case
    :return:{
                "msg":{
                    "rs":[
                        {
                            "case_title":"员工列表页面数据查看",
                            "module":"7186",
                            "precondition":"进入erp员工列表页面",
                            "pri":"1"
                        },
                        {
                            "case_title":"员工列表搜索查询",
                            "module":"7186",
                            "precondition":null,
                            "pri":"2"
                        }],
                    "title":"机构后台回归测试用例"
                },
                "status":0
            }

    """
    # ZenTaoBusiness.zt_case_sync()
    return None
